package com.mew.chess.core.game.lh.game.core;


import cn.hutool.core.util.RandomUtil;
import com.mew.chess.core.common.message.response.ChipMessage;
import com.mew.chess.core.common.utils.RobotNumberUtils;
import com.mew.chess.core.game.lh.game.LhGameManager;
import com.mew.chess.core.game.lh.game.LhRoomManager;
import com.mew.chess.core.game.lh.game.common.LhBetType;
import com.mew.chess.core.game.lh.game.common.LhConstantValue;
import com.mew.chess.core.game.lh.game.message.LhPushBetInfoMessage;
import com.mew.chess.core.game.lh.game.message.LhUserMessage;
import com.mew.common.util.GameUtil;

import java.util.Collections;
import java.util.List;

public class LhRobotBetUtils {

    //求和信息
    public static LhPushBetInfoMessage sumBetNumber(int roomId, boolean isRobot){
        LhRoomManager roomManager = LhGameManager.getInstance().getController(roomId);
        //清一下上次押注数组
        LhPushBetInfoMessage betInfoMessage = roomManager.getBetInfoMessage();
        betInfoMessage.clearInfoList();

        if (isRobot){
            List<LhUserMessage> robotPlayers = roomManager.getRobotPlayers();
            buildRobotBetInfo(roomManager,robotPlayers,roomManager.getBetArrays());
        }

        return betInfoMessage;

    }

    private static void buildRobotBetInfo(LhRoomManager roomManager, List<LhUserMessage> robotPlayers, long[] betArrays){

        Collections.shuffle(robotPlayers);

        int robotNumber = RobotNumberUtils.robotNumber(LhConstantValue.ROBOT_BET_NUMBER);

        for (int i = 0; i < robotNumber; i++) {
            LhUserMessage lhUserMessage = robotPlayers.get(i);
            long bet = betArrays[GameUtil.weights(LhConstantValue.ROBOT_BET_WEIGHTS)];
            long gold = lhUserMessage.getGoldNumber()-bet;
            if (gold<0){
                continue;
            }
            int type = RandomUtil.randomInt(LhBetType.values().length);
            ChipMessage chipMessage = ChipMessage.builder().userId(lhUserMessage.getUserId()).betType(type).betNumber(bet).build();
            lhUserMessage.getBets().add(chipMessage);

            lhUserMessage.setGoldNumber(gold<0?0:gold);

            roomManager.getBetInfoMessage().addBetInfo(lhUserMessage.getUserId(),type,bet);
        }

    }
}
